package org.example.collect;

import java.util.Arrays;
import java.util.Collections;

public class Arr{

    private Integer[] elements;

    public Arr(Integer[] elements){
        this.elements=elements;
    }

    public Integer[] getElements() {
        return elements;
    }

    public void print(boolean isToString){
        if(!isToString){
            System.out.println("元素包括：");
            for(int ele:elements){
                System.out.print(ele);
                System.out.print("   ");
            }
        }
        else{
            System.out.println("\r\n-------------");
            System.out.println("元素包括：");
            System.out.print(Arrays.toString(elements));
            System.out.println("\r\n-------------");
        }
    }

    public int max(){
        int max = elements[0];
        for(int i=1;i<elements.length;i++){
            if(elements[i].compareTo(max)==1){
                max=elements[i];
            }
        }
        return max;
    }

    public void removeByIdx(int idx){
        int maxIdx=elements.length-1;
        if(idx>maxIdx){
            return;
        }
        Integer[] arrNew = new Integer[maxIdx];
        int maxNewIdx = arrNew.length-1;
        for(int i=0;i<idx;i++){
            arrNew[i]=elements[i];
        }
        for(int i=idx;i<=maxNewIdx;i++){
            arrNew[i]=elements[i+1];
        }
        elements=arrNew;
    }

    public static Integer[] remove(Integer[] ori,Integer element){
        Integer[] target = new Integer[ori.length];
        int idx=0;
        for(int i=0;i<ori.length;i++){
            if(element.equals(ori[i])){
                continue;
            }
            target[idx]=ori[i];
            idx++;
        }
        int newlen = idx;
        Integer[] res = new Integer[newlen];
        for(int i=0; i < newlen; i++){
            res[i] = target[i];
        }
        return res;
    }

    public void remove2(Integer[] ori,Integer element){
        Integer[] target = new Integer[ori.length];
        int idx=0;
        for(int i=0;i<ori.length;i++){
            if(element.equals(ori[i])){
                continue;
            }
            target[idx]=ori[i];
            idx++;
        }
        int newlen = idx;
        Integer[] res = new Integer[newlen];
        for(int i=0; i < newlen; i++){
            res[i] = target[i];
        }
        elements=res;
    }

    public static Integer[] copy(Integer[] ori){
        Integer[] target = new Integer[ori.length];
        for(int i=0;i<ori.length;i++){
            target[i]=ori[i];
        }
        return target;
    }

    public static Integer[] copy2(Integer[] ori){
        return Arrays.copyOf(ori,ori.length);
    }

    public void add(int idx,Integer ele){
        int maxIdx=elements.length-1;
        Integer[] arrNew = new Integer[elements.length+1];
        int maxNewIdx = arrNew.length-1;
        if(idx>maxIdx){
            for(int i=0;i<=maxIdx;i++){
                arrNew[i]=elements[i];
            }
            arrNew[maxIdx+1]=ele;
        }
        else{
            for(int i=0;i<idx;i++){
                arrNew[i]=elements[i];
            }
            arrNew[idx]=ele;
            for(int i=idx+1;i<=maxNewIdx;i++){
                arrNew[i]=elements[i-1];
            }
        }
        elements=arrNew;
    }

    public void asc(){
        Arrays.sort(elements);
    }

    public void desc(){
        Arrays.sort(elements, Collections.<Integer>reverseOrder());
    }

}